package vda

import (
	"errors"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"vrcm/src/vrcm/app/admin/models"
	"vrcm/src/vrcm/common/service"
	ext "vrcm/src/vrcm/envconfig"
)

var client = http.Client{}

type VdaService struct {
	service.Service
}

func (e *VdaService) ConCurrentConnections(tenantId string, vdaType string) (string, error) {

	urlPath := ext.ExtConfig.ServiceConfig.VdaServiceUrl + "/globalLive/vrbm/concurrentConnections?tenant_id=" + tenantId + "&related_product=" + vdaType + "&vip=videosolar"
	e.Log.Trace("ConCurrentConnections path:", urlPath)
	// 获取 request请求
	request, err := http.NewRequest("GET", urlPath, nil)
	if len(ext.ExtConfig.ServiceConfig.VsApiServiceUrl) > 0 {
		request.Header.Set("Authorization", "Bearer "+models.SuperToken)
		request.Header.Set("X-PS-Flag", "")
	} else {
		request.Header.Set("Authorization", "vsnb")
	}
	request.Header.Add("Content-Type", "application/json")
	if err != nil {
		e.Log.Errorf("GetHttpSkip Request Error:", err)
		return "", err
	}
	response, err := client.Do(request)
	if err != nil {
		e.Log.Errorf("GetHttpSkip Response Error:", err)
		return "", err
	}

	defer response.Body.Close()
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		e.Log.Errorf("读取响应体失败 error:%s", err)
		return "", err
	}
	e.Log.Trace("ConCurrentConnections:", string(body))
	result := gjson.Parse(string(body))
	if result.Get("code").Int() != http.StatusOK {
		return "", errors.New(result.Get("message").String())
	}

	return result.Get("data").Get("value").String(), nil
}
